﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

using SPBusiness.ExtensionMethods;
using Microsoft.SharePoint.Taxonomy;

namespace SPBusiness
{
   public sealed class List
    {
              /// <summary>
       /// List class has only Static Methods, no constructor needed.
       /// </summary>
       private List() { }

        public static void SetSPListNavigateForFormsPages(string pSiteUrl, string pWebUrl, string pListTitle, bool pNavigateForFormsPages)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {
                            SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                            if (vSPList != null)
                            {
                                if (vSPList.NavigateForFormsPages != pNavigateForFormsPages)
                                {
                                    vSPList.NavigateForFormsPages = pNavigateForFormsPages;
                                    vSPList.Update();
                                    vSPWeb.Update();
                                }
                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "NavigateForFormsPages", ex);
                throw;
            }
        }

        public static void SetSPListWriteSecurity(string pSiteUrl, string pWebUrl, string pListTitle, int pWriteSecurity)
        {
            try
            {

                //SPSecurity.RunWithElevatedPrivileges(delegate()
       // {

            using (SPSite vSPSite = new SPSite(pSiteUrl))
            {
                using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                {
                    SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                    if (vSPList != null)
                    {
                        if (vSPList.WriteSecurity != pWriteSecurity)
                        {
                            vSPList.WriteSecurity = pWriteSecurity;
                            vSPList.Update();
                            vSPWeb.Update();
                        }
                    }
                }
            }
        //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetSPListWriteSecurity", ex);
                throw;
            }
        }

        public static void SetSPListReadSecurity(string pSiteUrl, string pWebUrl, string pListTitle, int pReadSecurity)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
     //   {

            using (SPSite vSPSite = new SPSite(pSiteUrl))
            {
                using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                {
                    SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                    if (vSPList != null)
                    {
                        if (vSPList.ReadSecurity != pReadSecurity)
                        {
                            vSPList.ReadSecurity = pReadSecurity;
                            vSPList.Update();
                            vSPWeb.Update();
                        }
                    }
                }
            }
        //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetSPListReadSecurity", ex);
                throw;
            }
        }

        public static void SetSPListPermission(string pSiteUrl, string pWebUrl, string pListTitle, string pGroupName, string pRole)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
              //  {
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {
                        SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);
                        SPGroup vSPGroup = vSPWeb.SiteGroups[pGroupName];

                        if (vSPList != null)
                        {
                            if (!vSPList.HasUniqueRoleAssignments)
                            {
                                vSPList.BreakRoleInheritance(true, true);
                            }

                            SPRoleAssignment vSPRoleAssignment = null;
                            SPRoleDefinition vSPRoleDefinition = null;

                            SPRoleDefinition vSPRoleDefinitionToAdd = vSPList.ParentWeb.RoleDefinitions[pRole];

                            bool vFound = false;

                            for (int k = vSPList.RoleAssignments.Count - 1; k >= 0; k--)
                            {
                                vSPRoleAssignment = vSPList.RoleAssignments[k];

                                if (vSPRoleAssignment.Member.ID == vSPGroup.ID)
                                {
                                    for (int i = vSPRoleAssignment.RoleDefinitionBindings.Count - 1; i >= 0; i--)
                                    {
                                        vSPRoleDefinition = vSPRoleAssignment.RoleDefinitionBindings[i];
                                        if (vSPRoleDefinition.Name != vSPRoleDefinitionToAdd.Name)
                                        {
                                            vSPRoleAssignment.RoleDefinitionBindings.Remove(i);
                                            vSPRoleAssignment.Update();
                                        }
                                        else
                                        {
                                            vFound = true;
                                        }
                                    }
                                    if (vSPRoleAssignment.RoleDefinitionBindings.Count == 0)
                                    {
                                        vSPList.RoleAssignments.Remove(k);
                                        vSPList.Update();
                                    }
                                }
                            }

                            if (!vFound)
                            {
                                vSPRoleAssignment = new SPRoleAssignment(vSPGroup);

                                vSPRoleAssignment.RoleDefinitionBindings.Add(vSPRoleDefinitionToAdd);

                                vSPList.RoleAssignments.Add(vSPRoleAssignment);

                                vSPList.Update();
                            }
                        }
                    }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetSPListPermission", ex);
                throw;
            }
        }

        public static void SetSPListPermission(string pSiteUrl, string pWebUrl, string pListTitle, string pGroupName, SPRoleType pSPRoleType)
        {
            try
            {
               //SPSecurity.RunWithElevatedPrivileges(delegate()
               // {
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {
                        using (SPWeb vSPWebGroup = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);
                            SPGroup vSPGroup = vSPWebGroup.SiteGroups[pGroupName];

                            if (vSPList != null)
                            {
                                if (!vSPList.HasUniqueRoleAssignments)
                                {
                                    vSPList.BreakRoleInheritance(true, true);
                                }

                                SPRoleAssignment vSPRoleAssignment = null;
                                SPRoleDefinition vSPRoleDefinition = null;

                                SPRoleDefinition vSPRoleDefinitionToAdd = vSPList.ParentWeb.RoleDefinitions.GetByType(pSPRoleType);

                                bool vFound = false;

                                for (int k = vSPList.RoleAssignments.Count - 1; k >= 0; k--)
                                {
                                    vSPRoleAssignment = vSPList.RoleAssignments[k];

                                    if (vSPRoleAssignment.Member.ID == vSPGroup.ID)
                                    {
                                        for (int i = vSPRoleAssignment.RoleDefinitionBindings.Count - 1; i >= 0; i--)
                                        {
                                            vSPRoleDefinition = vSPRoleAssignment.RoleDefinitionBindings[i];
                                            if (vSPRoleDefinition.Name != vSPRoleDefinitionToAdd.Name)
                                            {
                                                vSPRoleAssignment.RoleDefinitionBindings.Remove(i);
                                                vSPRoleAssignment.Update();
                                            }
                                            else
                                            {
                                                vFound = true;
                                            }
                                        }
                                        if (vSPRoleAssignment.RoleDefinitionBindings.Count == 0)
                                        {
                                            vSPList.RoleAssignments.Remove(k);
                                            vSPList.Update();
                                        }
                                    }
                                }

                                if (!vFound)
                                {
                                    vSPRoleAssignment = new SPRoleAssignment(vSPGroup);

                                    vSPRoleAssignment.RoleDefinitionBindings.Add(vSPRoleDefinitionToAdd);

                                    vSPList.RoleAssignments.Add(vSPRoleAssignment);

                                    vSPList.Update();
                                }
                            }
                        }
                    }
                }
                       //});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetSPListPermission", ex);
                throw;
            }
        }

        public static void SetUniqueField(string pSiteUrl, string pWebUrl, string pListName, string pStaticFieldName, bool vUnique)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
              //  {
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {
                        SPField vSPFieldSite = null;

                        if (vSPWeb.IsRootWeb)
                        {
                            vSPFieldSite = vSPWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                        }
                        else
                        {
                            vSPFieldSite = vSPWeb.Site.RootWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                        }

                        if (vSPFieldSite != null)
                        {
                            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                            if (vSPList != null)
                            {
                                if (vSPList.Fields.OfType<SPField>().Where(x => x.StaticName == vSPFieldSite.StaticName).Any())
                                {
                                    SPField vSPField = vSPList.Fields[vSPFieldSite.Id];

                                    if (vSPField.EnforceUniqueValues != vUnique)
                                    {
                                        vSPField.EnforceUniqueValues = vUnique;
                                        vSPField.Update();
                                        vSPList.Update();
                                    }

                                }
                            }

                        }
                    }
                }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetUniqueField", ex);
                throw;
            }
        }

        public static void SetMultiValueField(string pSiteUrl, string pWebUrl, string pListName, string pStaticFieldName, bool vMultiValue)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
              //  {
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {
                            SPField vSPFieldSite = null;

                            if (vSPWeb.IsRootWeb)
                            {
                                vSPFieldSite = vSPWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                            }
                            else
                            {
                                vSPFieldSite = vSPWeb.Site.RootWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                            }

                            if (vSPFieldSite != null)
                            {
                                SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                                if (vSPList != null)
                                {
                                    if (vSPList.Fields.OfType<SPField>().Where(x => x.StaticName == vSPFieldSite.StaticName).Any())
                                    {
                                        SPFieldLookup vSPFieldLookup = vSPList.Fields[vSPFieldSite.Id] as SPFieldLookup;

                                        if (vSPFieldLookup != null && vSPFieldLookup.AllowMultipleValues != vMultiValue)
                                        {
                                            vSPFieldLookup.AllowMultipleValues = vMultiValue;
                                            vSPFieldLookup.Update();
                                            vSPList.Update();
                                        }

                                    }
                                }

                            }
                        }
                    }
                //});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetMultiValueField", ex);
                throw;
            }
        }       

        public static void SetRequiredField(SPWeb pSPWeb, string pListName, string pStaticFieldName, bool vRequired)
        {
            try
            {
                using (SPSite vSPSite = new SPSite(pSPWeb.Site.Url))
                {
                    using (SPWeb vSPWeb = vSPSite.OpenWeb(pSPWeb.ID))
                    {
                        SPField vSPFieldSite = null;

                        if (vSPWeb.IsRootWeb)
                        {
                            vSPFieldSite = vSPWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                        }
                        else
                        {
                            vSPFieldSite = vSPWeb.Site.RootWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                        }

                        if (vSPFieldSite != null)
                        {
                            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                            if (vSPList != null)
                            {
                                if (vSPList.Fields.OfType<SPField>().Where(x => x.StaticName == vSPFieldSite.StaticName).Any())
                                {
                                    SPField vSPField = vSPList.Fields[vSPFieldSite.Id];

                                    if (vSPField.Required != vRequired)
                                    {
                                        vSPField.Required = vRequired;
                                        vSPField.Update();
                                        vSPList.Update();
                                    }

                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetRequiredField", ex);
                throw;
            }
        }

        public static void SetFieldDefaultValue(string pSiteUrl, string pWebUrl, string pListName, string pStaticFieldName, string vDefaultValue)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                     //  {

                           using (SPSite vSPSite = new SPSite(pSiteUrl))
                           {
                               using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                               {
                                   SPField vSPFieldSite = null;

                                   if (vSPWeb.IsRootWeb)
                                   {
                                       vSPFieldSite = vSPWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                                   }
                                   else
                                   {
                                       vSPFieldSite = vSPWeb.Site.RootWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                                   }

                                   if (vSPFieldSite != null)
                                   {
                                       SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                                       if (vSPList != null)
                                       {
                                           if (vSPList.Fields.OfType<SPField>().Where(x => x.StaticName == vSPFieldSite.StaticName).Any())
                                           {
                                               SPField vSPField = vSPList.Fields[vSPFieldSite.Id];

                                               if (vSPField.DefaultValue != vDefaultValue)
                                               {
                                                   vSPField.DefaultValue = vDefaultValue;
                                                   vSPField.Update();
                                                   vSPList.Update();
                                               }

                                           }
                                       }

                                   }
                               }
                           }
                       //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetFieldDefaultValue", ex);
                throw;
            }
        }

        public static void SetShowFieldinDisplayForm(SPWeb pSPWeb, string pListName, string pFieldStaticName, bool pShow)
        {
            try
            {
                using (SPSite vSPSite = new SPSite(pSPWeb.Site.Url))
                {
                    using (SPWeb vSPWeb = vSPSite.OpenWeb(pSPWeb.ID))
                    {

                        SPField vSPField;
                        SPList vSPList;

                        vSPList = vSPWeb.Lists.TryGetList(pListName);
                        if (vSPList != null)
                        {
                            vSPField = vSPList.Fields.TryGetFieldByStaticName(pFieldStaticName);

                            if (vSPField != null)
                            {
                                if (vSPField.ShowInDisplayForm != pShow)
                                {
                                    vSPField.ShowInDisplayForm = pShow;
                                    vSPField.Update();
                                    vSPList.Update();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetShowFieldinDisplayForm", ex);
                throw;
            }
        }

        public static void SetShowFieldinEditForm(SPWeb pSPWeb, string pListName, string pFieldStaticName, bool pShow)
        {
            try
            {
                using (SPSite vSPSite = new SPSite(pSPWeb.Site.Url))
                {
                    using (SPWeb vSPWeb = vSPSite.OpenWeb(pSPWeb.ID))
                    {

                        SPField vSPField;
                        SPList vSPList;

                        vSPList = vSPWeb.Lists.TryGetList(pListName);
                        if (vSPList != null)
                        {
                            vSPField = vSPList.Fields.TryGetFieldByStaticName(pFieldStaticName);

                            if (vSPField != null)
                            {
                                if (vSPField.ShowInEditForm != pShow)
                                {
                                    vSPField.ShowInEditForm = pShow;
                                    vSPField.Update();
                                    vSPList.Update();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetShowFieldinEditForm", ex);
                throw;
            }
        }

        public static void SetShowFieldinNewForm(SPWeb pSPWeb, string pListName, string pFieldStaticName, bool pShow)
        {
            try
            {
                using (SPSite vSPSite = new SPSite(pSPWeb.Site.Url))
                {
                    using (SPWeb vSPWeb = vSPSite.OpenWeb(pSPWeb.ID))
                    {

                        SPField vSPField;
                        SPList vSPList;

                        vSPList = vSPWeb.Lists.TryGetList(pListName);
                        if (vSPList != null)
                        {
                            vSPField = vSPList.Fields.TryGetFieldByStaticName(pFieldStaticName);

                            if (vSPField != null)
                            {
                                if (vSPField.ShowInNewForm != pShow)
                                {
                                    vSPField.ShowInNewForm = pShow;
                                    vSPField.Update();
                                    vSPList.Update();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetShowFieldinNewForm", ex);
                throw;
            }
        }

        public static void AddContentType(string pSiteUrl, string pWebUrl, string pListName, string pContentTypeName)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
//{

    using (SPSite vSPSite = new SPSite(pSiteUrl))
    {
        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
        {


            if (vSPWeb.AvailableContentTypes[(pContentTypeName)] != null)
            {
                SPContentType vSPContentType = null;
                vSPContentType = vSPWeb.AvailableContentTypes[pContentTypeName];

                SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                if (vSPList != null)
                {

                    if (!vSPList.ContentTypesEnabled)
                    {
                        vSPList.ContentTypesEnabled = true;
                        vSPList.Update();
                    }

                    var vContentTypes = from vContentType in vSPList.ContentTypes.OfType<SPContentType>()
                                        select new { vContentType.NameResource };

                    if (!vContentTypes.Any(x => x.NameResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033)) == vSPContentType.NameResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033))))
                    {
                        vSPList.ContentTypes.Add(vSPContentType);
                        vSPList.Update();
                    }

                    SPFolder vSPFolder = vSPList.RootFolder;
                    //IList<SPContentType> vSPContentTypeList = vSPList.ContentTypes.OfType<SPContentType>().ToList();
                    //vSPFolder.UniqueContentTypeOrder = vSPContentTypeList;
                    //vSPFolder.Update();

                    IList<SPContentType> vSPContentTypeList = vSPFolder.ContentTypeOrder;

                    if (!vSPContentTypeList.Any(x => x.NameResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033)) == pContentTypeName))
                    {                
                        vSPContentType = vSPList.ContentTypes.OfType<SPContentType>().FirstOrDefault(x => x.NameResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033)) == pContentTypeName);                
                        vSPContentTypeList.Add(vSPContentType);
                        vSPFolder.UniqueContentTypeOrder = vSPContentTypeList;
                        vSPFolder.Update();
                    }

                    //Assegura que quaisquer pendências de update no objeto web sejam sanadas
                    vSPWeb.Update();
                }

            }
        }
    }
//});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "AddContentType", ex);
                throw;
            }
        }

        public static void RemoveContentType(string pSiteUrl, string pWebUrl, string pListName, string pContentTypeName)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
//{

    using (SPSite vSPSite = new SPSite(pSiteUrl))
    {
        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
        {

            if (vSPWeb.AvailableContentTypes[(pContentTypeName)] != null)
            {
                SPContentType vSPContentType = null;
                vSPContentType = vSPWeb.AvailableContentTypes[pContentTypeName];

                SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                if (vSPList != null)
                {

                    if (!vSPList.ContentTypesEnabled)
                    {
                        vSPList.ContentTypesEnabled = true;
                        vSPList.Update();
                    }

                    var vContentTypes = from vContentType in vSPList.ContentTypes.OfType<SPContentType>()
                                        where vContentType.NameResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033)) == vSPContentType.NameResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033))
                                        select new { vContentType.Id, vContentType.Name };


                    if (vContentTypes.Count() > 0)
                    {
                        vSPList.ContentTypes.Delete(vContentTypes.FirstOrDefault().Id);
                        vSPList.Update();
                    }

                    SPFolder vSPFolder = vSPList.RootFolder;
                    IList<SPContentType> vSPContentTypeList = vSPFolder.ContentTypeOrder;

                    if (vSPContentTypeList != null && vSPContentTypeList.Count > 0)
                    {
                        if (vSPContentTypeList.Any(x => x.NameResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033)) == pContentTypeName))
                        {
                            vSPContentTypeList.Remove(vSPContentTypeList.FirstOrDefault(x => x.NameResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033)) == pContentTypeName));
                            vSPFolder.UniqueContentTypeOrder = vSPContentTypeList;
                            vSPFolder.Update();
                        }
                    }


                    //Assegura que quaisquer pendências de update no objeto web sejam sanadas
                    vSPWeb.Update();
                }
            }
        }
    }
//});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "RemoveContentType", ex);
                throw;
            }
        }
     
        public static void ChangeFieldDisplayName(string pSiteUrl, string pWebUrl, string pListName, string pFieldStaticName, string pFieldDisplayNameNew, System.Globalization.CultureInfo pCultureInfo)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
               // {

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            System.Threading.Thread.CurrentThread.CurrentUICulture = pCultureInfo;

                            SPField vSPField;
                            SPList vSPList;


                            vSPList = vSPWeb.Lists.TryGetList(pListName);
                            if (vSPList != null)
                            {
                                vSPField = vSPList.Fields.TryGetFieldByStaticName(pFieldStaticName);


                                if (vSPField != null)
                                {
                                    vSPField.Title = pFieldDisplayNameNew;
                                     vSPField.PushChangesToLists = true;
                                    vSPField.Update(true);
                                    vSPList.Update();
                                }
                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "ChangeFieldDisplayName", ex);
                throw;
            }
        }

        public static void ChangeSPListFieldDescription(string pSiteUrl, string pWebUrl, string pListName, string pFieldStaticName, string pFieldDescriptionNew)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
               // {

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {


                            SPField vSPField;
                            SPField vSPFieldSite;
                            SPList vSPList;

                            vSPList = vSPWeb.Lists.TryGetList(pListName);
                            if (vSPList != null)
                            {
                                vSPField = vSPList.Fields.TryGetFieldByStaticName(pFieldStaticName);

                                vSPFieldSite = vSPList.ParentWeb.Site.RootWeb.Fields.TryGetFieldByStaticName(pFieldStaticName);

                                if (vSPField != null)
                                {
                                    vSPField.Description = pFieldDescriptionNew;

                                    if (vSPFieldSite != null)
                                    {

                                        vSPField.SchemaXml = vSPField.SchemaXml.Replace("Description=\"" + vSPFieldSite.DescriptionResource.GetValueForUICulture(new System.Globalization.CultureInfo(1033)) + "\"", "Description=\"" + pFieldDescriptionNew + "\"");
                                        
                                    }

                                    vSPField.Update();

                                    vSPField.DescriptionResource.SetValueForUICulture(new System.Globalization.CultureInfo(1033), pFieldDescriptionNew);
                                    
                                    vSPField.DescriptionResource.Update();

                                    vSPList.Update();
                                }
                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "ChangeSPListFieldDescription", ex);
                throw;
            }
        }

        public static void ConfigView(string pSiteUrl, string pWebUrl, string pListName, string pViewName, string pQuery, string[] pViewFields, SPViewScope pSPViewScope, bool pIsDefault, string vToolBar)
        {
            try
            {

                //SPSecurity.RunWithElevatedPrivileges(delegate()
//{

    using (SPSite vSPSite = new SPSite(pSiteUrl))
    {
        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
        {

            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

            if (vSPList != null)
            {

                SPView vSPView = vSPList.TryGetView(pViewName);

                if (vSPView != null)
                {

                    if (vSPView.Query != pQuery)
                    {
                        vSPView.Query = pQuery;
                        vSPView.Update();
                        vSPList.Update();
                    }

                    if ((vSPView.DefaultView != pIsDefault) || (vSPView.MobileDefaultView != pIsDefault))
                    {
                        vSPView.DefaultView = pIsDefault;
                        vSPView.MobileDefaultView = pIsDefault;
                        vSPView.Update();
                        vSPList.Update();
                    }

                    if (vSPView.Scope != pSPViewScope)
                    {
                        vSPView.Scope = pSPViewScope;
                        vSPView.Update();
                        vSPList.Update();
                    }

                    if (vSPView.Toolbar != vToolBar)
                    {
                        vSPView.Toolbar = vToolBar;
                        vSPView.Update();
                        vSPList.Update();
                    }

                    IEnumerable<string> vIEnumerableString = vSPView.ViewFields.ToStringCollection().OfType<string>();

                    if (string.Join(",", vIEnumerableString.ToArray()) != string.Join(",", pViewFields))
                    {
                        vSPView.ViewFields.DeleteAll();

                        foreach (string pViewField in pViewFields)
                        {
                            vSPView.ViewFields.Add(pViewField);
                        }

                        vSPView.Update();
                        vSPList.Update();

                    }

                    //Assegura que quaisquer pendências de update no objeto web sejam sanadas
                    vSPWeb.Update();
                }
            }
        }
    }
//});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "ConfigView", ex);
                throw;
            }
        }

        public static void ConfigView(string pSiteUrl, string pWebUrl, string pListName, string pViewName, string pQuery, string[] pViewFields, SPViewScope pSPViewScope, bool pIsDefault)
        {
            try
            {

                //SPSecurity.RunWithElevatedPrivileges(delegate()
               // {

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                            if (vSPList != null)
                            {

                                SPView vSPView = vSPList.TryGetView(pViewName);

                                if (vSPView != null)
                                {

                                    if (vSPView.Query != pQuery)
                                    {
                                        vSPView.Query = pQuery;
                                        vSPView.Update();
                                        vSPList.Update();
                                    }

                                    if ((vSPView.DefaultView != pIsDefault) || (vSPView.MobileDefaultView != pIsDefault))
                                    {
                                        vSPView.DefaultView = pIsDefault;
                                        vSPView.MobileDefaultView = pIsDefault;
                                        vSPView.Update();
                                        vSPList.Update();
                                    }

                                    if (vSPView.Scope != pSPViewScope)
                                    {
                                        vSPView.Scope = pSPViewScope;
                                        vSPView.Update();
                                        vSPList.Update();
                                    }

                                    IEnumerable<string> vIEnumerableString = vSPView.ViewFields.ToStringCollection().OfType<string>();

                                    if (string.Join(",", vIEnumerableString.ToArray()) != string.Join(",", pViewFields))
                                    {
                                        vSPView.ViewFields.DeleteAll();

                                        foreach (string pViewField in pViewFields)
                                        {
                                            vSPView.ViewFields.Add(pViewField);
                                        }

                                        vSPView.Update();
                                        vSPList.Update();

                                    }

                                    //Assegura que quaisquer pendências de update no objeto web sejam sanadas
                                    vSPWeb.Update();
                                }
                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "ConfigView", ex);
                throw;
            }
        }

        public static void SetDefaultView(string pSiteUrl, string pWebUrl, string pListName, string pViewName)
        {
            try
            {

                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                            if (vSPList != null)
                            {

                                var vCustomSPViews = from vSPView in vSPList.Views.OfType<SPView>()
                                                     select new { vSPView.ID, vSPView.Url };

                                foreach (var vCustomSPView in vCustomSPViews.ToList())
                                {
                                    SPView vSPView = vSPList.Views.TryGetViewtByID(vCustomSPView.ID);
                                    if (vSPView != null)
                                    {
                                        vSPView.DefaultView = false;
                                        vSPView.Update();
                                    }
                                }


                                vSPList.Update();
                                vSPWeb.Update();

                                SPView vNewDefaultSPView = vSPList.Views.TryGetViewtByTitle(pViewName);

                                if (vNewDefaultSPView != null)
                                {
                                    vNewDefaultSPView.DefaultView = true;
                                    vNewDefaultSPView.MobileDefaultView = true;
                                    vNewDefaultSPView.Update();

                                    vSPList.Update();
                                    vSPWeb.Update();
                                }


                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetDefaultView", ex);
                throw;
            }
        }

        public static void AddView(string pSiteUrl, string pWebUrl, string pListName, string pViewDisplayName, string pQuery, string[] pViewFields, SPViewScope pSPViewScope, bool pIsDefault, string vToolbar)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
//{

    using (SPSite vSPSite = new SPSite(pSiteUrl))
    {
        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
        {

            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

            if (vSPList != null)
            {

                SPView vSPView = null;

                if (!vSPList.Views.Cast<SPView>().Any(x => x.Title == pViewDisplayName))
                {

                    StringCollection vStringCollection = new StringCollection();
                    vStringCollection.AddRange(pViewFields);


                    string pViewName = pViewDisplayName.PrepareToUrl();

                    vSPView = vSPList.Views.Add(pViewName, vStringCollection, pQuery, 30, true, pIsDefault);
                    vSPView.Scope = pSPViewScope;
                    vSPView.Update();

                    vSPView.Title = pViewDisplayName;
                    vSPView.Toolbar = vToolbar;
                    vSPView.Update();

                    vSPList.Update();

                    if ((vSPView.DefaultView != pIsDefault) || (vSPView.MobileDefaultView != pIsDefault))
                    {
                        vSPView.DefaultView = pIsDefault;
                        vSPView.MobileDefaultView = pIsDefault;
                        vSPView.Update();

                        vSPList.Update();
                    }

                    //Assegura que quaisquer pendências de update no objeto web sejam sanadas
                    vSPWeb.Update();
                }
                else
                {
                    ConfigView(pSiteUrl, pWebUrl, pListName, pViewDisplayName, pQuery, pViewFields, pSPViewScope, pIsDefault, vToolbar);

                }

            }
        }
    }
//});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "AddView", ex);
                throw;
            }
        }

        public static void AddView(string pSiteUrl, string pWebUrl, string pListName, string pViewDisplayName, string pViewPageName, string pQuery, string[] pViewFields, SPViewScope pSPViewScope, bool pIsDefault)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
               // {

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                            if (vSPList != null)
                            {

                                SPView vSPView = null;

                                if (!vSPList.Views.Cast<SPView>().Any(x => x.Title == pViewDisplayName))
                                {

                                    StringCollection vStringCollection = new StringCollection();
                                    vStringCollection.AddRange(pViewFields);


                                    vSPView = vSPList.Views.Add(pViewPageName, vStringCollection, pQuery, 30, true, pIsDefault);
                                    vSPView.Scope = pSPViewScope;
                                    vSPView.Update();

                                    vSPView.Title = pViewDisplayName;
                                     vSPView.Update();

                                    vSPList.Update();

                                    if ((vSPView.DefaultView != pIsDefault) || (vSPView.MobileDefaultView != pIsDefault))
                                    {
                                        vSPView.DefaultView = pIsDefault;
                                        vSPView.MobileDefaultView = pIsDefault;
                                        vSPView.Update();

                                        vSPList.Update();
                                    }

                                    //Assegura que quaisquer pendências de update no objeto web sejam sanadas
                                    vSPWeb.Update();
                                }
                                else
                                {
                                    ConfigView(pSiteUrl, pWebUrl, pListName, pViewDisplayName, pQuery, pViewFields, pSPViewScope, pIsDefault);

                                }

                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "AddView", ex);
                throw;
            }
        }

        public static void DeleteView(string pSiteUrl, string pWebUrl, string pListName, string pViewDisplayName)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                            if (vSPList != null)
                            {
                                if (vSPList.Views.OfType<SPView>().Any(x => x.Title == pViewDisplayName))
                                {

                                    SPView vSPView = vSPList.Views.OfType<SPView>().FirstOrDefault(x => x.Title == pViewDisplayName);
                                    string vUrl = vSPView.Url;
                                    vSPList.Views.Delete(vSPView.ID);
                                    vSPList.Update();

                                    SPFile vSPFile = vSPWeb.TryGetFileByUrl(vUrl);
                                    if (vSPFile != null) { vSPFile.Delete(); }
                                }
                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "DeleteView", ex);
                throw;
            }
        }

        public static void DeleteAllViews(string pSiteUrl, string pWebUrl, string pListName)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
               // {

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);


                            var vCustomSPViews = from vSPView in vSPList.Views.OfType<SPView>()
                                                 select new { vSPView.ID, vSPView.Url };

                            foreach (var vCustomSPView in vCustomSPViews.ToList())
                            {
                                try
                                {
                                    vSPList.Views.Delete(vCustomSPView.ID);
                                    vSPList.Update();
                                }
                                catch(Exception ex)
                                {
                                    Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "DeleteAllViews", "O seguinte erro ocorreu excluindo a view " + vCustomSPView.Url + Environment.NewLine + ex.Message);
                                }

                                SPFile vSPFile = vSPWeb.TryGetFileByUrl(vCustomSPView.Url);
                                if (vSPFile != null) { vSPFile.Delete(); }

                            }

                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "DeleteAllViews", ex);
                throw;
            }
        }

        public static void AddCallendarView(string pSiteUrl, string pWebUrl, string pListName, string pViewDisplayName, string pQuery, string[] pViewFields, SPViewScope pSPViewScope, bool pIsDefault, string vViewData)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
               // {

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                            if (vSPList != null)
                            {

                                SPView vSPView = null;

                                if (!vSPList.Views.Cast<SPView>().Any(x => x.Title == pViewDisplayName))
                                {

                                    StringCollection vStringCollection = new StringCollection();
                                    vStringCollection.AddRange(pViewFields);


                                    string pViewName = pViewDisplayName.PrepareToUrl();

                                    vSPView = vSPList.Views.Add(pViewName, vStringCollection, pQuery, 0, false, pIsDefault, SPViewCollection.SPViewType.Calendar, false);
                                    vSPView.Scope = pSPViewScope;
                                    vSPView.ViewData = vViewData;
                                    vSPView.Update();

                                    vSPView.Title = pViewDisplayName;
                                    vSPView.Update();

                                    vSPList.Update();

                                    if ((vSPView.DefaultView != pIsDefault) || (vSPView.MobileDefaultView != pIsDefault))
                                    {
                                        vSPView.DefaultView = pIsDefault;
                                        vSPView.MobileDefaultView = pIsDefault;
                                        vSPView.Update();

                                        vSPList.Update();
                                    }

                                    //Assegura que quaisquer pendências de update no objeto web sejam sanadas
                                    vSPWeb.Update();
                                }

                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "AddCallendarView", ex);
                throw;
            }
        }

        public static Int32 CountItens(string pSiteUrl, string pWebRelative, string pListTitle)
        {
            try
            {
                Int32 vReturn = 0;

                //SPSecurity.RunWithElevatedPrivileges(delegate()
              //  {

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelative))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                            if (vSPList != null)
                            {
                                vReturn = vSPList.ItemCount;
                            }

                        }
                    }
                //});

                return vReturn;
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "CountItens", ex);
                throw;
            }


        }

        public static void SetRequiredField(string pSiteUrl, string pWebUrl, string pListName, string pStaticFieldName, bool vRequired)
        {
            try
            {

                //SPSecurity.RunWithElevatedPrivileges(delegate()
              //  {

                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {
                            SPField vSPFieldSite = null;

                            if (vSPWeb.IsRootWeb)
                            {
                                vSPFieldSite = vSPWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                            }
                            else
                            {
                                vSPFieldSite = vSPWeb.Site.RootWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                            }

                            if (vSPFieldSite != null)
                            {


                                SPList vSPList = vSPWeb.Lists.TryGetList(pListName);

                                if (vSPList != null)
                                {
                                    if (vSPList.Fields.OfType<SPField>().Any(x => x.Id == vSPFieldSite.Id))
                                    {
                                        SPField vSPField = vSPList.Fields[vSPFieldSite.Id];

                                        if (vSPField.Required != vRequired)
                                        {
                                            vSPField.Required = vRequired;
                                            vSPField.Update();
                                            vSPList.Update();
                                            vSPWeb.Update();
                                        }
                                    }

                                }
                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetRequiredField", ex);
                throw;
            }
        }

        public static void SetDefaultDisplayListForms(string pSiteUrl, string pWebRelativeUrl, string pListTitle, string pContentTypeName, string pFormRelativeUrl)
        {
           try
           {
              //SPSecurity.RunWithElevatedPrivileges(delegate()
            //  {
                 using (SPSite vSPSite = new SPSite(pSiteUrl))
                 {
                    using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                    {

                       SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                       if (vSPList != null)
                       {
                          if (!System.String.IsNullOrEmpty(vSPList.DefaultDisplayFormUrl))
                          {
                             SPFile vSPFile = vSPWeb.TryGetFileByUrl(vSPList.DefaultDisplayFormUrl);
                             if (vSPFile != null) { vSPFile.Delete(); }
                             vSPList.DefaultDisplayFormUrl = pFormRelativeUrl;
                             SPContentType vSPContentType = vSPList.ContentTypes[pContentTypeName];
                             SPContentType vSPContentTypeWeb = vSPWeb.ContentTypes[pContentTypeName];
                             vSPContentType.DisplayFormUrl = pFormRelativeUrl;
                             vSPContentTypeWeb.DisplayFormUrl = pFormRelativeUrl;
                             vSPContentType.Update();
                             vSPContentTypeWeb.Update();
                             vSPList.Update();
                             vSPWeb.Update();
                          }                        
                       }
                    }
                 }
              //});
           }
           catch (Exception ex)
           {
              Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetDefaultDisplayListForms", ex);
              throw;
           }

        }
        public static void SetDefaultEditListForms(string pSiteUrl, string pWebRelativeUrl, string pListTitle, string pContentTypeName, string pFormRelativeUrl)
        {
           try
           {
              //SPSecurity.RunWithElevatedPrivileges(delegate()
             // {
                 using (SPSite vSPSite = new SPSite(pSiteUrl))
                 {
                    using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                    {

                       SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                       if (vSPList != null)
                       {                          
                          if (!System.String.IsNullOrEmpty(vSPList.DefaultEditFormUrl))
                          {
                             SPFile vSPFile = vSPWeb.TryGetFileByUrl(vSPList.DefaultEditFormUrl);
                             if (vSPFile != null) { vSPFile.Delete(); }
                             vSPList.DefaultEditFormUrl = pFormRelativeUrl;
                             
                             SPContentType vSPContentType = vSPList.ContentTypes[pContentTypeName];
                             SPContentType vSPContentTypeWeb = vSPWeb.ContentTypes[pContentTypeName];
                             vSPContentType.EditFormUrl = pFormRelativeUrl;
                             vSPContentTypeWeb.EditFormUrl = pFormRelativeUrl;
                             vSPContentType.Update();
                             vSPContentTypeWeb.Update();
                             vSPList.Update();
                             vSPWeb.Update();
                          }     
                       }
                    }
                 }
              //});
           }
           catch (Exception ex)
           {
              Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetDefaultDisplayListForms", ex);
              throw;
           }

        }
        public static void SetDefaultNewListForms(string pSiteUrl, string pWebRelativeUrl, string pListTitle, string pContentTypeName, string pFormRelativeUrl)
        {
           try
           {
              //SPSecurity.RunWithElevatedPrivileges(delegate()
             // {
                 using (SPSite vSPSite = new SPSite(pSiteUrl))
                 {
                    using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                    {

                       SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                       if (vSPList != null)
                       {                          
                          if (!System.String.IsNullOrEmpty(vSPList.DefaultNewFormUrl))
                          {
                             SPFile vSPFile = vSPWeb.TryGetFileByUrl(vSPList.DefaultNewFormUrl);
                             if (vSPFile != null) { vSPFile.Delete(); }
                             vSPList.DefaultNewFormUrl = pFormRelativeUrl;

                             SPContentType vSPContentType = vSPList.ContentTypes[pContentTypeName];
                             SPContentType vSPContentTypeWeb = vSPWeb.ContentTypes[pContentTypeName];
                             vSPContentType.NewFormUrl = pFormRelativeUrl;
                             vSPContentTypeWeb.NewFormUrl = pFormRelativeUrl;
                             vSPContentType.Update();
                             vSPContentTypeWeb.Update();
                             vSPList.Update();
                             vSPWeb.Update();
                          }
                       }
                    }
                 }
              //});
           }
           catch (Exception ex)
           {
              Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "SetDefaultDisplayListForms", ex);
              throw;
           }

        }

        public static void DeleteListForms(string pSiteUrl, string pWebRelativeUrl, string pListTitle)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
               // {
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                            if (vSPList != null)
                            {
                                if (!System.String.IsNullOrEmpty(vSPList.DefaultDisplayFormUrl))
                                {
                                    SPFile vSPFile = vSPWeb.TryGetFileByUrl(vSPList.DefaultDisplayFormUrl);
                                    if (vSPFile != null) { vSPFile.Delete(); }
                                }
                                if (!System.String.IsNullOrEmpty(vSPList.DefaultEditFormUrl))
                                {
                                    SPFile vSPFile = vSPWeb.TryGetFileByUrl(vSPList.DefaultEditFormUrl);
                                    if (vSPFile != null) { vSPFile.Delete(); }
                                }
                                if (!System.String.IsNullOrEmpty(vSPList.DefaultNewFormUrl))
                                {
                                    SPFile vSPFile = vSPWeb.TryGetFileByUrl(vSPList.DefaultNewFormUrl);
                                    if (vSPFile != null) { vSPFile.Delete(); }
                                }

                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "DeleteListForms", ex);
                throw;
            }

        }

        public static void DeleteListFile(string pSiteUrl, string pWebRelativeUrl, string pListTitle, string pFileName)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pListTitle);

                            if (vSPList != null)
                            {
                                SPFile vSPFile = null;

                                if (vSPList.BaseType == SPBaseType.DocumentLibrary)
                                {
                                    vSPFile = vSPWeb.TryGetFileByUrl(vSPList.RootFolder.ServerRelativeUrl + "/forms/" + pFileName);
                                }
                                else
                                {
                                    vSPFile = vSPWeb.TryGetFileByUrl(vSPList.RootFolder.ServerRelativeUrl + "/" + pFileName);
                                }

                                if (vSPFile != null) { vSPFile.Delete(); }

                            }
                        }
                    }
                //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "DeleteListFile", ex);
                throw;
            }

        }

        public static void EnsureCleanupFormFiles(string vSiteUrl, string vWebUrl, string vListTile)
        {
           try
           {
              List.DeleteListForms(vSiteUrl, vWebUrl, vListTile);
              List.DeleteListFile(vSiteUrl, vWebUrl, vListTile, "DispForm.aspx");
              List.DeleteListFile(vSiteUrl, vWebUrl, vListTile, "EditForm.aspx");
              List.DeleteListFile(vSiteUrl, vWebUrl, vListTile, "NewForm.aspx");
           }
           catch (Exception ex)
           {
              Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "EnsureCleanupFormFiles", ex);
              throw;
           }
        }
        public static bool AddContentTypeAddView(string pSiteUrl, string pWebUrl, string pListTitle, string pContentTypeName, bool pDisableFolders, string pViewDisplayName, string pViewPageName, string pQuery, SPViewScope pSPViewScope, bool pIsDefault, bool pNavigateForFormsPages, params string[] pViewFields)
                                                                                                                                                                    
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {
                            List.AddContentType(pSiteUrl, pWebUrl, pListTitle, pContentTypeName);

                            List.RemoveContentType(pSiteUrl, pWebUrl, pListTitle, "Item");
                            List.RemoveContentType(pSiteUrl, pWebUrl, pListTitle, "Document");
                            if (pDisableFolders)
                            {
                                List.RemoveContentType(pSiteUrl, pWebUrl, pListTitle, "Folder");
                            }

                            List.AddView(pSiteUrl, pWebUrl, pListTitle, pViewDisplayName, pViewPageName, pQuery, pViewFields, pSPViewScope, pIsDefault);
                            List.SetSPListNavigateForFormsPages(pSiteUrl, pWebUrl, pListTitle, pNavigateForFormsPages);
                        }
                    }
                //});

                return true;
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(List).Namespace, typeof(List).Name, "AddContentTypeAddView", ex);
                throw;
            }
        }
  
    }
}
